﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Settlement : MonoBehaviour {

	Config config;

	public Sprite GameOverImg;
	public Sprite PassImg;
    public Sprite AllPassImg;

	void Awake() {
		config = GameObject.Find("/ConfigCube").GetComponent<Config> ();
	}

	void Start () {
		GameObject obj = gameObject.GetComponent<RectTransform> ().Find ("Title").gameObject;
		obj.GetComponent<Image> ().sprite = config.Status == 0 ? GameOverImg : ( config.Chapter >= Config.gameConfig.ChapterNum ? AllPassImg :  PassImg);
	}

	private int i = 0;  // 计算顺序
	private float _timer = 0.1f;  // 每秒刷新十次画面

	int num = -1;

	private bool ok1 = false;
	private bool ok2 = false;

    private int score1 = 0;
    private int score2 = 0;

	void Update() {

		_timer -= Time.deltaTime;
		if (_timer <= 0) {

			num++;
            
			if (ok1 == false) {
				GameObject obj = gameObject.GetComponent<RectTransform> ().Find ("Num" + i + "0").gameObject;
				obj.GetComponent<Text> ().text = "" + num;
                
                if(num>0) score1 = score1 + Config.gameConfig.EnemyScore[ i ];

                obj = gameObject.GetComponent<RectTransform> ().Find ("score1").gameObject;
                obj.GetComponent<Text> ().text = "玩家1 得分" + score1;
			}

			if (ok2 == false) {
				GameObject obj = gameObject.GetComponent<RectTransform> ().Find ("Num" + i + "1").gameObject;
				obj.GetComponent<Text> ().text = "" + num;

                if(num>0) score2 = score2 + Config.gameConfig.EnemyScore[ i ];

                obj = gameObject.GetComponent<RectTransform> ().Find ("score2").gameObject;
                obj.GetComponent<Text> ().text = "玩家2 得分" + score2;
			}

			if (num >= config.ScorePoint [i, 0]) {
				ok1 = true;
			}

			if (num >= config.ScorePoint [i, 1]) {
				ok2 = true;
			}

			if (ok1 && ok2) {
				i++;
				if (i == 4) {
					Time.timeScale = 0;
				} else {
					gameObject.GetComponent<RectTransform> ().Find ("right" + i + "0").gameObject.SetActive(true);
					gameObject.GetComponent<RectTransform> ().Find ("right" + i + "1").gameObject.SetActive(true);
					gameObject.GetComponent<RectTransform> ().Find ("Num" + i + "0").gameObject.SetActive(true);
					gameObject.GetComponent<RectTransform> ().Find ("Num" + i + "1").gameObject.SetActive(true);
					ok1 = false;
					ok2 = false;
					num = -1;
				}
			}

			_timer = 0.1f;
		}

		if (Time.timeScale == 0) {

			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < 2; j++) {
					config.ScorePoint [i, j] = 0;
				}
			}

			if (Input.anyKeyDown) {
				Time.timeScale = 1;
				if (config.Status == 0) {  // 游戏结束
					Destroy(GameObject.Find("/ConfigCube"));
					SceneManager.LoadScene ("UI");
				} else {
					config.Chapter++;
					config.Player1Num++;
					config.Player2Num++;

					config.Save ();

					if (config.Chapter > Config.gameConfig.ChapterNum) {

						print ("游戏通关········");
						Destroy(GameObject.Find("/ConfigCube"));
						SceneManager.LoadScene ("UI");
					} else {
						SceneManager.LoadScene ("GAME");
					}
				}
			}
		}
	}
}
